/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;
import constants.Constants;
import entity.Customer;
import java.awt.Desktop;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileFilter;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
public class ExportExcel {
    private  Workbook wb;
    private Row row;
    private  Sheet sheet;
    private  File file;
    public void exportTable(String tableName){
        
    }
    private  void prepare(String sheetName){
                wb=new HSSFWorkbook();
                int nos=wb.getNumberOfSheets();
		for (int i = 0; i < nos; i++) {
			String sn=wb.getSheetName(i);
			if(sn.equalsIgnoreCase(sheetName)){
				sheetName+="$1";
                                System.out.println(sheetName);
			}
		}
		//tạo worksheet
		sheet = wb.createSheet(sheetName);
                row = sheet.createRow((short)0);
		for (int i = 0; i < 4; i++) {
			Cell cell=row.createCell(i);
			cell.setCellValue("test");
		}
    
    }  
    private  void writeFile(File file){
        
        try (FileOutputStream fos = new FileOutputStream(file,true)) {
                    try {
                        wb.write(fos);
                    } catch (IOException ex) {
                        Validate.message="Có lỗi";
                        
                    }
                }catch(Exception e){
                        Validate.message="Có lỗi";
                
                }
    }
    public void insertRoom(){
    }  
    private  void insertCustomer(ArrayList<entity.Customer> list){
         String[] name ={"Mã khách hàng","Họ tên","Tuổi","Giới tính","CMND","Địa chỉ","Số điện thoại","Quốc tịch","Số phòng","Ngày đặt","Ngày trả","Tổng tiền"};        
        for(int j=0;j<=list.size();j++){	
                        Cell cell;
			row=sheet.createRow(j);                   
                   	for (int i = 0; i < 12; i++) {
                             if(j==0){
                             cell=row.createCell(i);
                             cell.setCellValue(name[i]);
                             }
                         else
                         switch(i){  
                            case 0: 
                                cell=row.createCell(i);
				cell.setCellValue(list.get(j-1).getId());break;
                            case 1: 
                                cell=row.createCell(i);
				cell.setCellValue(list.get(j-1).getName());break;
                            case 2: 
                                cell=row.createCell(i);
				cell.setCellValue(list.get(j-1).getAge());break;
                            case 3: 
                                cell=row.createCell(i);
				cell.setCellValue(Constants.GENDER[list.get(j-1).getGender()]);break;
                            case 4:  
                                cell=row.createCell(i);
				cell.setCellValue(list.get(j-1).getId_card());break;
                            case 5:
                                cell=row.createCell(i);
				cell.setCellValue(list.get(j-1).getAddress());break;
                            case 6:
                                cell=row.createCell(i);
				cell.setCellValue( list.get(j-1).getPhone());break;                                
                            case 7: 
                                cell=row.createCell(i);
				cell.setCellValue(list.get(j-1).getCountry());break;
                            case 8:
                                cell=row.createCell(i);
                                cell.setCellValue(list.get(j-1).getInfo().getNumber_room());break;
                            case 9:
                                cell=row.createCell(i);
                                cell.setCellValue(list.get(j-1).getInfo().getCheckin());break;
                            case 10:
                                cell=row.createCell(i);
                                cell.setCellValue(list.get(j-1).getInfo().getCheckout());break;
                            case 11 :
                                cell=row.createCell(i);
                                cell.setCellValue(list.get(j-1).getInfo().getTotal());break;
                         }
				
			}
		}
    }
    public void customer(ArrayList<Customer> list,String sheetName,File file){
                prepare(sheetName);
        	insertCustomer(list);
                writeFile(file);
                openFile(file.getPath());
    }
    private  void insertStatistic(ArrayList<entity.Statistic> list){
                  String[] name ={"Ngày","Số lượt đặt phòng","số lượt trả phòng","doanh thu"};
          
                for(int j=0;j<=list.size();j++){
                        Cell cell;
			row=sheet.createRow(j);                       
			for (int i = 0; i < 4; i++) {
                            if(j==0){
                                cell=row.createCell(i);
				cell.setCellValue(name[i]);
                            }
                          else  
                         switch(i){   
                            case 0: 
                                cell=row.createCell(i);
				cell.setCellValue(list.get(j-1).getDate());break;
                            case 1: 
                                cell=row.createCell(i);
				cell.setCellValue(list.get(j-1).getBookingN());break;
                            case 2: 
                                cell=row.createCell(i);
				cell.setCellValue(list.get(j-1).getCheckoutN());break;
                            case 3: 
                                cell=row.createCell(i);
				cell.setCellValue(list.get(j-1).getRevenue());break;
                                
                            }
				
			}
		}
    }
    public  void statistic(ArrayList<entity.Statistic> list, String sheetName,File file){
                prepare(sheetName);
        	insertStatistic(list);
                writeFile(file);
                openFile(file.getPath());
    }
    private void openFile(String path){    
      try {
                    Desktop desktop = null;
                    if (Desktop.isDesktopSupported()) {
                        desktop = Desktop.getDesktop();
                    }
                        desktop.open(new File(path));
                    } catch (IOException ioe) {
                        System.out.println(ioe.toString());
      }
    }
    public boolean choose(){
           
        JFileChooser fc = new JFileChooser();
        int save;
        fc.setDialogTitle("Export file");
        save=fc.showSaveDialog(null);
      
        if(save==JFileChooser.APPROVE_OPTION){
              file = fc.getSelectedFile();

                 return true;
        }
        return false;
        
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

 

    
}
